<?php

namespace components;
/**
 * Base functionality for components
 *
 */
abstract class Component extends \components\Attribute /*implements \interfaces\Component*/
{
	/**
	 * Component's xml in HtmlElement object
	 */
	public $component;

	/**
	 * Final result of component's work
	 */
	private $result = "";

	/**
	 * Parsed children list
	 */
	private $children = array();

	/**
	 * Form name encountered in component
	 */
	private $form = "";

	final public function  __construct(\core\HtmlElement $xml)
	{
		$this->component = $xml;
		$this->SetUpAttributes();
	}

	public function Complete()
	{
		$result = $this->GetResult();

		$res = "";

		if (is_array($result))
		{
			foreach ($result as $item)
			{
				$res .= $item->Build();
			}
		}
		elseif (is_a($result, "\core\HtmlElement"))
		{
			$res = $result->Build();
		}
		else
		{
			$res = $result;
		}

		return $res;
	}

	protected function SetResult($result)
	{
		$this->result = $result;
	}

	protected function GetResult()
	{
		return $this->result;
	}

	protected function GetComponent()
	{
		return $this->component;
	}

	protected function GetParsedChildren()
	{
		return $this->children;
	}

	protected function GetChildren()
	{
		$children = $this->GetComponent()->GetChildren();

		$res = array();

		if (!empty($children) && empty($this->children))
		{
			foreach ($children as $item)
			{
				$tag = $item->GetTag();

				$componentType = "component"; //or operation

				if($tag == "field")
				{
					$this->form = $item->GetAttribute('form');
				}

				$res[] = component($item);
			}
		}
		else
		{
			$res = $this->children;
		}

		return $res;
	}

	/*protected function ResetChildren()
	{
	$this->children = array();
	}*/
/*
	protected function ParseChildren()
	{
		$children = $this->GetComponent()->GetChildren();

		foreach ($children as $item)
		{
			$tag = $item->GetTag();

			$componentType = "component"; //or operation

			if($tag == "field")
			{
				$this->form = $item->GetAttribute('form');
			}

			$this->children[] = component($item);
		}

		return true;
	}*/

	protected function GetForm()
	{
		return $this->form;
	}

	protected function ClearForm()
	{
		$this->form = "";
		return true;
	}

}